﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TestHelpers.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   The test helpers.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Collaboris.DataAccess.Test
{
    using System;
    using System.Diagnostics;

    using Microsoft.SharePoint;

    /// <summary>
    /// The test helpers.
    /// </summary>
    internal class TestHelpers
    {
        #region Public Methods

        /// <summary>
        /// Creates a Sharepoint List
        /// </summary>
        /// <param name="web">
        /// The web.
        /// </param>
        /// <param name="template">
        /// The template.
        /// </param>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <param name="description">
        /// The description.
        /// </param>
        /// <param name="overwrite">
        /// The overwrite.
        /// </param>
        /// <returns>
        /// Guid for the newly created list. If the list already exists and Overwrite
        /// if false, then the existing GUID is returned.
        /// </returns>
        public static Guid CreateList(SPWeb web, string template, string name, string description, bool overwrite)
        {
            string listParamsMsg = "Web : " + web.Url + Environment.NewLine + " Template : " + template +
                                   Environment.NewLine + " Name : " + name + Environment.NewLine + " Description : " +
                                   description + Environment.NewLine + " Overwrite : " + overwrite;

            if (ListExists(web, name))
            {
                Guid existListGuid = GetListGuid(web, name);

                if (existListGuid == Guid.Empty)
                {
                    throw new NullReferenceException(
                        "Failed to return the GUID for an Existing List. ListName = " + name);
                }

                if (overwrite == false)
                {
                    return existListGuid;
                }
                else
                {
                    DeleteList(web, existListGuid);
                }
            }

            // attempt to return the list template
            SPListTemplate listTemplate = web.ListTemplates[template];

            Guid listGuid = web.Lists.Add(name, description, listTemplate);
            web.Update();

            return listGuid;
        }

        /// <summary>
        /// Deletes a list
        /// </summary>
        /// <param name="web">
        /// </param>
        /// <param name="listGuid">
        /// </param>
        public static void DeleteList(SPWeb web, Guid listGuid)
        {
            try
            {
                if (ListExists(web, listGuid))
                {
                    web.Lists.Delete(listGuid);
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError("Error found in SPListHelper.DeleteList. Error ToString() : '{0}'", ex.ToString());
            }
        }

        /// <summary>
        /// The delete list.
        /// </summary>
        /// <param name="web">
        /// The web.
        /// </param>
        /// <param name="listUrl">
        /// The list url.
        /// </param>
        public static void DeleteList(SPWeb web, string listUrl)
        {
            DeleteList(web, listUrl, false);
        }

        /// <summary>
        /// Deletes the list.
        /// </summary>
        /// <param name="web">
        /// The web.
        /// </param>
        /// <param name="listName">
        /// The list Name.
        /// </param>
        /// <param name="listItemsOnly">
        /// The list Items Only.
        /// </param>
        public static void DeleteList(SPWeb web, string listName, bool listItemsOnly)
        {
            Guid guidList = GetListGuid(web, listName);
            if (listItemsOnly)
            {
                DeleteListItems(guidList, web);
            }
            else
            {
                if (ListExists(web, guidList))
                {
                    web.Lists.Delete(guidList);
                }
            }
        }

        /// <summary>
        /// Gets a List GUID based on a list name and SPWeb
        /// </summary>
        /// <param name="web">
        /// SPWeb
        /// </param>
        /// <param name="listName">
        /// List Name
        /// </param>
        /// <returns>
        /// The Guid of the list; otherwise an empty Guid if not found.
        /// </returns>
        public static Guid GetListGuid(SPWeb web, string listName)
        {
            try
            {
                Guid listGuid;

                SPList list = web.Lists[listName];

                if (list != null)
                {
                    listGuid = list.ID;

                    return listGuid;
                }

                throw new ApplicationException("Failed to find the list : " + listName);
            }
            catch (Exception ex)
            {
                Trace.TraceError("Error found in SPListHelper.GetListGuid. Error ToString() : '{0}'", ex.ToString());
                return Guid.Empty;
            }
        }

        /// <summary>
        /// Checks to see if a list exists in a given Lists Collection.
        /// </summary>
        /// <param name="web">
        /// SPWeb containing the Lists collection.
        /// </param>
        /// <param name="listGuid">
        /// Guid representing the list
        /// </param>
        /// <returns>
        /// true if it does exits; otherwise false.
        /// </returns>
        public static bool ListExists(SPWeb web, Guid listGuid)
        {
            try
            {
                SPList list = web.Lists[listGuid];

                if (list != null)
                {
                    return true;
                }

                return false;
            }
            catch (Exception ex)
            {
                Trace.TraceError("Error found in SPListHelper.ListExists. Error ToString() : '{0}'", ex.ToString());

                return false;
            }
        }

        /// <summary>
        /// Checks to see if a list exists in a given Lists Collection.
        /// </summary>
        /// <param name="web">
        /// SPWeb containing the Lists collection.
        /// </param>
        /// <param name="listName">
        /// string Name of the list
        /// </param>
        /// <returns>
        /// true if it does exits; otherwise false.
        /// </returns>
        public static bool ListExists(SPWeb web, string listName)
        {
            try
            {
                SPList list = web.Lists[listName];

                if (list != null)
                {
                    return true;
                }

                return false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        #endregion

        #region Helper Methods

        /// <summary>
        /// Deletes the list items.
        /// </summary>
        /// <param name="listGuid">
        /// The list GUID.
        /// </param>
        /// <param name="web">
        /// The web.
        /// </param>
        private static void DeleteListItems(Guid listGuid, SPWeb web)
        {
            SPList list = web.Lists[listGuid];

            SPListItemCollection coll = list.Items;
            int colCount = coll.Count;
            for (int i = 0; i < colCount; i++)
            {
                coll.Delete(0);
            }
        }

        #endregion
    }
}